package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.DistrictDto;
import com.returnsoft.resource.eao.DistrictEao;
import com.returnsoft.resource.entity.District;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class DistrictBean
 */
@Stateless
@LocalBean
public class DistrictBean implements DistrictInterface {

	@EJB
	private DistrictEao districtEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public DistrictBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<DistrictDto> findByProvince(Integer provinceId)
			throws RecruitmentException {
		try {
			List<District> districts = districtEao.findByProvince(provinceId);
			List<DistrictDto> districtsDto = new ArrayList<DistrictDto>();
			for (District district : districts) {
				DistrictDto districtDto = conv.fromDistrict(district);
				districtsDto.add(districtDto);
			}
			return districtsDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
